﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;
using KorbeRTR.Utility;

namespace KorbeRTR
{
    public partial class FormErrorList : Form
    {
        public FormErrorList()
        {
            InitializeComponent();
        }

        private void SetDisable()
        {
            tsAdd.Enabled = false;

        }

        private void FormErrorList_Load(object sender, EventArgs e)
        {
            //更新treeview
            UpdateTree(treeView1);

            //init故障代码表
            InitGrid(grid1);
            UpdateGrid(grid1);
        }

        private void UpdateTree(TreeView tree)
        {
            tree.Nodes.Clear();

            var list = AlarmCode.ListAllAlarmType();

            TreeNode root = new TreeNode();
            root.Name = "root";
            root.Text = "故障类型";
            tree.Nodes.Add(root);
            foreach (var iter in list)
            {
                TreeNode node = new TreeNode();
                node.Name = iter;
                node.Text = iter;
                root.Nodes.Add(node);
            }
            tree.ExpandAll();
        }

        private void InitGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;
            grid.DisplayRowNumber = true;

            grid.Cols = 9;
            grid.Rows = 11;

            grid.Column(0).Width = 30;
            grid.Column(5).Width = 230;

            grid.Column(7).CellType = FlexCell.CellTypeEnum.CheckBox;
            int col = 0;
            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "单元号";
            grid.Cell(0, col++).Text = "位地址";
            grid.Cell(0, col++).Text = "绝对地址";
            grid.Cell(0, col++).Text = "故障代码";
            grid.Cell(0, col++).Text = "故障内容";
            grid.Cell(0, col++).Text = "故障类型";
            grid.Cell(0, col++).Text = "是否处理";
            grid.Cell(0, col++).Text = "发生槽号";
           

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGridByList(FlexCell.Grid grid, List<AlarmCode> list)
        {
            grid.AutoRedraw = false;
            grid.Rows = list.Count + 1;
            for (int i = 0; i < list.Count; i++)
            {
                int col = 1;
                grid1.Cell(i + 1, col++).Text = list[i].Addr.ToString();
                grid1.Cell(i + 1, col++).Text = list[i].SubAddr.ToString();
                grid1.Cell(i + 1, col++).Text = list[i].AbsAddr.ToString();
                grid1.Cell(i + 1, col++).Text = list[i].Code.ToString();
                grid1.Cell(i + 1, col++).Text = list[i].Content;
                grid1.Cell(i + 1, col++).Text = list[i].AlarmType;
                grid1.Cell(i + 1, col++).Text = list[i].Handle ? "1" : "0";
                grid1.Cell(i + 1, col++).Text = list[i].Slot.ToString();

            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGrid(FlexCell.Grid grid)
        {
            var list = AlarmCode.ListAll();
            UpdateGridByList(grid, list);
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left)
            {
                return;
            }
            if (e.Node.Level == 0)//0级结点
            {
                UpdateGrid(grid1);
            }
            else
            {
                var type = e.Node.Text;
                var list = AlarmCode.SelectByAlarmType(type);
                UpdateGridByList(grid1, list);
            }
        }


        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        private void tsChange_Click(object sender, EventArgs e)
        {
          

            int addr = (int)grid1.Cell(grid1.Selection.FirstRow, 1).Text.ToDouble();
            int subaddr = (int)grid1.Cell(grid1.Selection.FirstRow, 2).Text.ToDouble();

            var code = AlarmCode.SelectWhereAddrAndSubAddr(addr, subaddr);
            if (code == null)
            {
                MessageBox.Show(string.Format("未找到此故障代码{0}.{1}", addr, subaddr));
                return;
            }

            bool bl = CheckForm(typeof(FormError修改).Name);
            if (!bl)
            {
                FormError修改 form = new FormError修改(code);
                form.ShowDialog();
                UpdateGrid(grid1);
            }
        }
    }
}
